<!DOCTYPE html>
<!--
     SPDX-License-Identifier: CC-BY-SA-4.0
     SPDX-FileCopyrightText: 2020 seL4 Project a Series of LF Projects, LLC.
-->
<!-- Page last generated 2025-02-20 03:16:15 +0000 -->
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Hello, World! | seL4 docs</title>

    <!-- Our stylesheet and theme stylesheet.  Contains bootstrap. -->
    <link rel="stylesheet" href="/assets/css/style.css" type="text/css">
    <!-- Font awesome -->
    <link href="https://use.fontawesome.com/releases/v5.0.8/css/all.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
    <!-- Pygments syntax highlighting  -->
    <link rel="stylesheet" href="/assets/css/highlighting/trac.css" type="text/css">
    <link rel="icon" type="image/x-icon" href="/assets/favicon.ico"><script defer data-domain="docs.sel4.systems"
	    src="https://analytics.sel4.systems/js/script.js"></script></head>

  <body class="container-fluid">

    



<header>
  <ul class="row menu">
    <li class="col-xs-12 col-md-2" >
            <a href="https://sel4.systems" class="skip-icon">
              <img class="img-responsive" src="/assets/logo-text-white.svg" alt="seL4 logo" />
            </a>
    </li>
    <li class="col-xs-12 col-md-10 menu">
      <nav aria-label="Banner links">
        <h2><a href="/Resources" />Resources</h2>
        <h2><a href="/processes" />Contributing</a></h2>
        <h2><a href="/projects" />Projects</h2>
        <h2><a href="/Tutorials" />Tutorials</h2>
        <iframe title="DuckDuckGo search bar" src="https://duckduckgo.com/search.html?site=docs.sel4.systems&prefill=Search%20sel4.systems" style="overflow:hidden;margin-bottom:10px; padding:0;height:40px;float:right;border-width: 0px"></iframe>
      </nav>
    </li>
  </ul>
  <div class="clear"></div>
  
<div class="breadcrumbs bootstrap hidden-sm-down">
  <nav class="sel-breadcrumb" aria-label="Breadcrumb" >
    <ol class=" list-unstyled" vocab="http://schema.org/" typeof="BreadcrumbList">
      
      
        

        

        <li class="breadcrumb-item" property="itemListElement" typeof="ListItem">
            <a property="item" typeof="WebPage" href="/">
              <span property="name"><b>seL4 Docs</b></span>
            </a>
            <meta property="position" content="1" />
        </li>
      
        

        

        <li class="breadcrumb-item" property="itemListElement" typeof="ListItem">
            <a property="item" typeof="WebPage" href="/Tutorials/">
              <span property="name"><b>Tutorials</b></span>
            </a>
            <meta property="position" content="2" />
        </li>
      
        

        
          <li class="breadcrumb-item" property="itemListElement" typeof="ListItem">
            <span property="name">Hello, World!</span>
            <meta property="position" content="3" /></li>
          
    </ol>
  </nav>
  <nav class="sel-version" aria-label="Current Versions">
    <ol class="list-unstyled">
      <li class="list-unstyled text-right" style="margin-left:auto; padding:0rem 0rem;">
        Current versions:</li>
      <li class="list-unstyled text-right">
      <a href="/releases/sel4/13.0.0"><b>seL4-13.0.0</b></a></li>
      <li class="list-unstyled text-right">
      <a href="/releases/microkit/1.4.1"><b>microkit-1.4.1</b></a></li>
      <li class="list-unstyled text-right">
      <a href="/releases/camkes/camkes-3.11.0"><b>camkes-3.11.0</b></a></li>
      <li class="list-unstyled text-right">
      <a href="/releases/capdl/0.3.0"><b>capDL-0.3.0</b></a></li>
      </ol>
  </nav>
  <div class='clear'></div>
</div>


</header>

    <main>
      <div class="row">
  <div class="hidden-xs col-sm-4 col-md-3 col-lg-2">
    

<div class="sidebar">
  <ul class="nav nav-sidebar tutorial-sidebar">
    <li class="nav-section">Getting started</li>
      <li><a href="/Tutorials/">Overview</a></li>
      <li><a href="/Tutorials/pathways">Tutorial pathways</a></li>
    <li class="nav-section">seL4</li>
      <li><a href="/Tutorials/setting-up">Setting up your machine</a></li>
      <li><a href="/Tutorials/get-the-tutorials">Getting the tutorials</a></li>
      <li><a href="/Tutorials/hello-world">Hello world</a></li>
      <li><a href="/Tutorials/capabilities">Capabilities</a></li>
      <li><a href="/Tutorials/untyped">Untyped</a></li>
      <li><a href="/Tutorials/mapping">Mapping</a></li>
      <li><a href="/Tutorials/threads">Threads</a></li>
      <li><a href="/Tutorials/ipc">IPC</a></li>
      <li><a href="/Tutorials/notifications">Notifications</a></li>
      <li><a href="/Tutorials/interrupts">Interrupts</a></li>
      <li><a href="/Tutorials/fault-handlers">Fault handling</a></li>
      <li><a href="/Tutorials/mcs">MCS extensions</a></li>
    <li class="nav-section">C Libraries</li>
      <li><a href="/Tutorials/libraries-1">Initialisation &amp; threading</a></li>
      <li><a href="/Tutorials/libraries-2">IPC</a></li>
      <li><a href="/Tutorials/libraries-3">Processes &amp; Elf loading</a></li>
      <li><a href="/Tutorials/libraries-4">Timer</a></li>
    <li class="nav-section">Microkit</li>
      <li><a href="https://trustworthy.systems/projects/microkit/tutorial/">Tutorial</a></li>
    <li class="nav-section">CAmkES</li>
      <li><a href="/Tutorials/hello-camkes-0">Hello CAmkES</a></li>
      <li><a href="/Tutorials/hello-camkes-1">Introduction to CAmkES</a></li>
      <li><a href="/Tutorials/hello-camkes-2">Events in CAmkES</a></li>
      <li><a href="/Tutorials/hello-camkes-timer">CAmkES timer tutorial</a></li>
      <li><a href="/Tutorials/camkes-vm-linux">CAmkES VM</a></li>
      <li><a href="/Tutorials/camkes-vm-crossvm">CAmkES cross-VM connectors</a></li>
    <li class="nav-section">Rust</li>
      <li><a href="https://github.com/seL4/rust-sel4">GitHub</a></li>
    <li class="nav-section">Resources</li>
      <li><a href="https://sel4.systems/Info/Docs/seL4-manual-latest.pdf">seL4 Manual</a></li>
      <li><a href="/projects/sel4/api-doc.html">seL4 API reference</a></li>
      <li><a href="/Tutorials/how-to">How to: a quick solutions guide</a></li>
      <li><a href="/projects/sel4-tutorials/debugging-guide">Debugging guide</a></li>
      <li><a href="/Resources#contact">Help contacts</a></li>
  </ul>
</div>

  </div>
  <div class="content col-sm-8 col-md-6 col-lg-7 main">

    
<!--
  Copyright 2017, Data61, CSIRO (ABN 41 687 119 230)

  SPDX-License-Identifier: BSD-2-Clause
-->

<h1 id="hello-world">Hello, World!</h1>

<p>In this tutorial you will:</p>

<ol>
  <li>Run Hello, World! to ensure your setup is working correctly</li>
  <li>Become familiar with the jargon <em>root task</em></li>
  <li>Build and simulate a seL4 project</li>
  <li>Have a basic understanding of the role of the <code class="language-plaintext highlighter-rouge">CMakeLists.txt</code> file in applications</li>
</ol>

<h2 id="prerequisites">Prerequisites</h2>
<ol>
  <li><a href="/Tutorials/setting-up">Set up your machine</a></li>
</ol>

<h2 id="building-your-first-program">Building your first program</h2>
<p>seL4 is a microkernel, not an operating system, and as a result only provides very minimal services.</p>

<p>After the kernel boots, an initial thread called the <em>root task</em> is started, which is then responsible for setting up the user-level system.</p>

<p>When the root task starts there are no available drivers, however a minimal C library is provided.</p>

<p>The tutorial is already set up to print “Hello, world!”, so at this point  all you need to do is build and run the tutorial.</p>

<h2 id="revisiting-containers">Revisiting containers</h2>

<p>We will use two terminals, as described in <a href="/Tutorials/setting-up#mapping-a-container">Setting up your machine</a>.</p>
<ul>
  <li>Terminal A is just a normal terminal, and is used for git operations, editing (e.g., vim, emacs), and other normal operations.</li>
  <li>Terminal B is running in a container, and is only used for compilation.</li>
</ul>

<p>This gives you the flexibility to use all the normal tools you are used to, while having the seL4 dependencies separated from your machine.</p>

<h3 id="create-a-container">Create a container</h3>

<p>Open a new terminal, Terminal B, to run a container.</p>

<p>Create a container:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>container
</code></pre></div></div>

<h2 id="initialising">Initialising</h2>

<p>Initialise the tutorial in Terminal B, which is running the container.</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># For instructions about obtaining the tutorial sources see https://docs.sel4.systems/Tutorials/get-the-tutorials</span>
<span class="c">#</span>
<span class="c"># Follow these instructions to initialise the tutorial</span>
<span class="c"># initialising the build directory with a tutorial exercise</span>
./init <span class="nt">--tut</span> hello-world
<span class="c"># building the tutorial exercise</span>
<span class="nb">cd </span>hello-world_build
ninja
</code></pre></div></div>

<p>This step creates two new directories in <code class="language-plaintext highlighter-rouge">sel4-tutorials-manifest</code>, namely <code class="language-plaintext highlighter-rouge">hello-world</code> and <code class="language-plaintext highlighter-rouge">hello-world_build</code></p>

<details>

  <summary><em>Hint:</em> tutorial solutions</summary>
  <p><br />
All tutorials come with complete solutions. To get solutions run:</p>

  <div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># For instructions about obtaining the tutorial sources see https://docs.sel4.systems/Tutorials/get-the-tutorials</span>
<span class="c">#</span>
<span class="c"># Follow these instructions to initialise the tutorial</span>
<span class="c"># initialising the build directory with a tutorial exercise</span>
./init <span class="nt">--solution</span> <span class="nt">--tut</span> hello-world
<span class="c"># building the tutorial exercise</span>
<span class="nb">cd </span>hello-world_build
ninja
</code></pre></div>  </div>

  <p>This will generate another <code class="language-plaintext highlighter-rouge">hello-world</code> directory and <code class="language-plaintext highlighter-rouge">hello-world_build</code> directory, with unique names, e.g. <code class="language-plaintext highlighter-rouge">hello-world44h1po5q</code> and <code class="language-plaintext highlighter-rouge">hello-world44h1po5q_build</code>.</p>

</details>

<h3 id="build-the-program">Build the program</h3>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd sel4-tutorials-manifest/hello-world_build
</code></pre></div></div>

<p>Next, build the program in Terminal B using ninja</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># In build directory</span>
ninja
</code></pre></div></div>

<p>If successful, you should see the final ninja rule passing, e.g.:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[150/150] objcopy kernel into bootable elf
</code></pre></div></div>

<h3 id="run-hello-world-using-qemu">Run Hello, World using QEMU</h3>

<p>The final image can be run by:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./simulate
</code></pre></div></div>

<p>This will run the result on an instance of the <a href="https://www.qemu.org">QEMU</a> simulator.</p>

<p>If everything has worked, you should see:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Booting all finished, dropped to user space

Hello, World!
</code></pre></div></div>

<p>After that output, there should be a capability violation and a stack dump,
because the program hasn’t properly cleaned up after itself yet. (This will come in later examples)</p>

<p><code class="language-plaintext highlighter-rouge">Ctrl-A, X</code> will terminate QEMU.</p>

<h2 id="looking-at-the-sources">Looking at the sources</h2>
<p>To look at the sources, open a new terminal, Terminal A:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd sel4-tutorials-manifest/hello-world
ls
</code></pre></div></div>

<p>In your tutorial directory, you will find the following files:</p>
<ul>
  <li><code class="language-plaintext highlighter-rouge">CMakeLists.txt</code> - the file that incorporates the root task into the wider seL4 build system.</li>
  <li><code class="language-plaintext highlighter-rouge">src/main.c</code> - the single source file for the initial task.</li>
  <li><code class="language-plaintext highlighter-rouge">hello-world.md</code> - A generated README for the tutorial.</li>
</ul>

<h3 id="cmakeliststxt"><code class="language-plaintext highlighter-rouge">CMakeLists.txt</code></h3>

<p>Every application and library in an seL4 project requires a <code class="language-plaintext highlighter-rouge">CMakeLists.txt</code> file in order to be
 incorporated into the project build system.</p>

<div class="language-cmake highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">include</span><span class="p">(</span><span class="si">${</span><span class="nv">SEL4_TUTORIALS_DIR</span><span class="si">}</span>/settings.cmake<span class="p">)</span>
<span class="nf">sel4_tutorials_regenerate_tutorial</span><span class="p">(</span><span class="si">${</span><span class="nv">CMAKE_CURRENT_SOURCE_DIR</span><span class="si">}</span><span class="p">)</span>

<span class="nb">cmake_minimum_required</span><span class="p">(</span>VERSION 3.7.2<span class="p">)</span>
<span class="c1"># declare the hello-world CMake project and the languages it is written in (just C)</span>
<span class="nb">project</span><span class="p">(</span>hello-world C ASM<span class="p">)</span>

<span class="c1"># In future tutorials, these setup steps will be replaced with</span>
<span class="c1"># sel4_tutorials_setup_roottask_tutorial_environment()</span>
<span class="nb">find_package</span><span class="p">(</span>seL4 REQUIRED<span class="p">)</span>
<span class="nb">find_package</span><span class="p">(</span>elfloader-tool REQUIRED<span class="p">)</span>
<span class="nb">find_package</span><span class="p">(</span>musllibc REQUIRED<span class="p">)</span>
<span class="nb">find_package</span><span class="p">(</span>util_libs REQUIRED<span class="p">)</span>
<span class="nb">find_package</span><span class="p">(</span>seL4_libs REQUIRED<span class="p">)</span>

<span class="nf">sel4_import_kernel</span><span class="p">()</span>
<span class="nf">elfloader_import_project</span><span class="p">()</span>

<span class="c1"># This sets up environment build flags and imports musllibc and runtime libraries.</span>
<span class="nf">musllibc_setup_build_environment_with_sel4runtime</span><span class="p">()</span>
<span class="nf">sel4_import_libsel4</span><span class="p">()</span>
<span class="nf">util_libs_import_libraries</span><span class="p">()</span>
<span class="nf">sel4_libs_import_libraries</span><span class="p">()</span>
<span class="nf">sel4_tutorials_import_libsel4tutorials</span><span class="p">()</span>

<span class="c1"># Name the executable and list source files required to build it</span>
<span class="nb">add_executable</span><span class="p">(</span>hello-world src/main.c<span class="p">)</span>

<span class="c1"># List of libraries to link with the application.</span>
<span class="nb">target_link_libraries</span><span class="p">(</span>hello-world
    sel4runtime sel4
    muslc utils sel4tutorials
    sel4muslcsys sel4platsupport sel4utils sel4debug<span class="p">)</span>

<span class="c1"># Tell the build system that this application is the root task. </span>
<span class="nb">include</span><span class="p">(</span>rootserver<span class="p">)</span>
<span class="nf">DeclareRootserver</span><span class="p">(</span>hello-world<span class="p">)</span>

</code></pre></div></div>

<h3 id="mainc"><code class="language-plaintext highlighter-rouge">main.c</code></h3>

<p>The main C is a very typical C file. For a basic root server application, the only requirement is that 
a <code class="language-plaintext highlighter-rouge">main</code> function is provided.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp">
</span><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"Hello, World!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<h2 id="making-a-change">Making a change</h2>

<p>Test making a change to <code class="language-plaintext highlighter-rouge">main.c</code> by adding a second printf to output <code class="language-plaintext highlighter-rouge">"Second hello\n"</code>.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"Hello, World!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>

    <span class="n">printf</span><span class="p">(</span><span class="s">"Second hello</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Once you have made your change, use Terminal B to rebuild the project.</p>

<p><em>Hint:</em> Remember to exit the QEMU simulator before rerunning the project with <code class="language-plaintext highlighter-rouge">ctrl-A,x</code></p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># In build directory</span>
ninja
</code></pre></div></div>
<p>Then run the simulator again:</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># In build directory</span>
./simulate
</code></pre></div></div>

<p>On success, you should see the following:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
Second hello

</code></pre></div></div>



<p style="text-align: right;">

      Next: <a href="/Tutorials/capabilities">Capabilities</a>
</p>

<hr>
    <p><em>Tutorial included from <a href="https://github.com/sel4/sel4-tutorials/blob/master/tutorials/hello-world/hello-world.md">github repo</a> <a href="https://github.com/sel4/sel4-tutorials/edit/master/tutorials/hello-world/hello-world.md">edit</a></em></p>
  </div>

  
    
<div class="sidebar-toc hidden-xs hidden-sm col-md-3 col-lg-3">
    <ul id="toc" class="section-nav">
<li class="toc-entry toc-h2"><a href="#prerequisites">Prerequisites</a></li>
<li class="toc-entry toc-h2"><a href="#building-your-first-program">Building your first program</a></li>
<li class="toc-entry toc-h2"><a href="#revisiting-containers">Revisiting containers</a>
<ul>
<li class="toc-entry toc-h3"><a href="#create-a-container">Create a container</a></li>
</ul>
</li>
<li class="toc-entry toc-h2"><a href="#initialising">Initialising</a>
<ul>
<li class="toc-entry toc-h3"><a href="#build-the-program">Build the program</a></li>
<li class="toc-entry toc-h3"><a href="#run-hello-world-using-qemu">Run Hello, World using QEMU</a></li>
</ul>
</li>
<li class="toc-entry toc-h2"><a href="#looking-at-the-sources">Looking at the sources</a>
<ul>
<li class="toc-entry toc-h3"><a href="#cmakeliststxt">CMakeLists.txt</a></li>
<li class="toc-entry toc-h3"><a href="#mainc">main.c</a></li>
</ul>
</li>
<li class="toc-entry toc-h2"><a href="#making-a-change">Making a change</a></li>
</ul>
</div>

  
</div>
<script src="/assets/js/toggle-markdown.js"></script>

    </main>
    


<footer class="site-footer">

  <h2 class="footer-heading">seL4 docs</h2>

  <div class="footer-col-wrapper">

    <div class="col-md-2">
      



<ul class="social-media-list">
  <li><a href="https://github.com/sel4"><i class="fab fa-github"></i> <span class="username">sel4</span></a></li><li><a href="https://github.com/sel4proj"><i class="fab fa-github"></i> <span class="username">sel4proj</span></a></li>
</ul>

    </div>

    <div class="col-md-8">
      <ul class="list-unstyled">
        <li>
          This site is for displaying seL4 related documentation.  Pull requests are welcome.
        </li>
        
          <li>
            Site last updated: Fri Feb 7 10:17:38 2025 +1100 ee78c8857c
          </li>
          <li>
                Page last updated: Mon Jan 6 18:52:01 2025 +1100 df31e5ccb8
          </li>
        
      </ul>
    </div>
    <div class="col-md-2">
<a href="https://github.com/seL4/docs/blob/master/Tutorials/hello-world.md">View page on GitHub</a>
      <br />
      <a href="https://github.com/seL4/docs/edit/master/Tutorials/hello-world.md">Edit page on GitHub</a>
      <br />
      <a href="/sitemap">Sitemap</a>
    </div>

  </div>

</footer>

  </body>
</html>
